<?php
/**
 * CakeCMS - 用户管理
 * 功能模块: 用户管理,登陆
 * 
 * @author zzdboy
 * @version 1.0
 */
class UsersController extends AppController {
	public $components = array (
			'CheckUserPermission',
			'AdminLog' 
	);
	
	/**
	 * 初始化执行
	 *
	 * @see AppController::beforeFilter()
	 */
	public function beforeFilter() {
		$this->set ( 'title_for_layout', '用户管理' );
	}
	
	/**
	 * 登陆
	 */
	public function login() {
		$this->set ( 'title_for_layout', '用户登录' );
		
		// 如果已提交登录,则验证并进行登录操作
		if ($this->request->isPost ()) {
			// 验证表单数据
			if (empty ( $this->data ['username'] )) {
				$this->flash ( '用户名不能为空!', '/Users/login/', 1250 );
				return false;
			}
			
			if (empty ( $this->data ['password'] )) {
				$this->flash ( '密码不能为空!', '/Users/login/', 1250 );
				return false;
			}
			
			if (empty ( $this->data ['verificationCode'] ) || ($this->data ['verificationCode'] != $this->Session->read ( 'admin_autonumber' ))) {
				$this->flash ( '验证码有误,请重试!', '/Users/login/', 1250 );
				return false;
			}
			
			// 加载Admin model
			$this->loadModel ( 'Admin' );
			$conditions = array (
					'username' => $this->data ['username'],
					'password' => md5 ( $this->data ['password'] ) 
			);
			
			$AdminInfo = $this->Admin->find ( 'first', array (
					'conditions' => $conditions 
			) );
			
			if (! empty ( $AdminInfo )) {
				
				// 登录成功,记录session信息
				$this->Session->write ( 'admin_uid', $AdminInfo ['Admin'] ['uid'] );
				$this->Session->write ( 'username', $AdminInfo ['Admin'] ['username'] );
				$this->Session->write ( 'admin_roleid', $AdminInfo ['Admin'] ['roleid'] );
				
				$this->loadModel ( "AdminRole" );
				$AdminRoleInfo = $this->AdminRole->GetAdminRoleByID ( $AdminInfo ['Admin'] ['roleid'] );
				
				// 记录操作日志
				$this->AdminLog->addLog ( $AdminRoleInfo ['AdminRole'] ['rolename'] . '"' . $AdminInfo ['Admin'] ['username'] . '"' . '登陆了后台.' );
				
				$this->flash ( '登陆成功！', '/', 1250 );
			} else {
				$this->flash ( '用户名或密码不正确!', '/Users/login/', 1250 );
				return false;
			}
		} else {
			// 如果登陆了就跳转到首页
			if ($this->Session->read ( "admin_uid" )) {
				$this->redirect ( "/" );
			}
			// 图片验证码路径

			$verificationImgUrl = '/Verifications/show?' . generateRand ( 12 );
			$this->set ( 'verificationImgUrl', $verificationImgUrl );
		}
	}
	
	/**
	 * 退出系统
	 */
	public function logout() {
		$this->loadModel ( "AdminRole" );
		$AdminRoleInfo = $this->AdminRole->GetAdminRoleByID ( $this->Session->read ( "admin_roleid" ) );
		// 记录操作日志
		$this->AdminLog->addLog ( $AdminRoleInfo ['AdminRole'] ['rolename'] . '"' . $this->Session->read ( "username" ) . '"' . '退出了后台.' );
		
		$this->Session->destroy ();
		$this->flash ( '安全退出!', '/Users/login/', 1250 );
	}
	
	/**
	 * 个人信息修改
	 */
	public function editinfo() {
		if ($this->request->isPost ()) {
			$data ['Admin'] ['realname'] = $this->data ['realname'];
			if (empty ( $data ['Admin'] ['realname'] )) {
				$this->flash ( '真实姓名不能为空!', '/Users/editinfo/', 1250 );
				return false;
			}
			$data ['Admin'] ['email'] = $this->data ['email'];
			if (empty ( $data ['Admin'] ['email'] )) {
				$this->flash ( '电子邮件不能为空!', '/Users/editinfo/', 1250 );
				return false;
			}
			$data ['Admin'] ['lang'] = $this->data ['lang'];
			if (empty ( $data ['Admin'] ['lang'] )) {
				$this->flash ( '请选择语言!', '/Users/editinfo/', 1250 );
				return false;
			}
			
			$data ['Admin'] ['uid'] = $this->Session->read ( 'admin_uid' );
			
			$this->loadModel ( "Admin" );
			if ($this->Admin->edit ( $data )) {
				
				// 记录操作日志
				$this->AdminLog->addLog ( '编辑个人信息,用户uid:' . $this->Session->read ( 'admin_uid' ) );
				
				$this->flash ( '修改个人信息成功!', '/Users/editinfo/', 1250 );
				return false;
			} else {
				$this->flash ( '修改个人信息失败!', '/Users/editinfo/', 1250 );
				return false;
			}
		} else {
			// 加载Admin model
			$this->loadModel ( 'Admin' );
			$AdminInfo = $this->Admin->GetAdminByID ( $this->Session->read ( 'admin_uid' ) );
			$AdminInfo ['Admin'] ['lastlogintime'] = date ( "Y-m-d H:i:s", $AdminInfo ['Admin'] ['lastlogintime'] );
			$this->set ( 'AdminInfo', $AdminInfo );
		}
	}
	
	/**
	 * 修改密码
	 */
	public function editpwd() {
		if ($this->request->isPost ()) {
			$old_password = $this->data ['old_password'];
			$new_password = $this->data ['new_password'];
			$new_pwdconfirm = $this->data ['new_pwdconfirm'];
			
			// 加载Admin model
			$this->loadModel ( 'Admin' );
			$AdminInfo = $this->Admin->GetAdminByID ( $this->Session->read ( 'admin_uid' ) );
			
			if (strtolower ( md5 ( $old_password ) ) != strtolower ( $AdminInfo ['Admin'] ['password'] )) {
				$this->flash ( '旧密码输入错误!', '/Users/editpwd/', 1250 );
				return false;
			}
			
			if (empty ( $new_password )) {
				$this->flash ( '请输入新密码!', '/Users/editpwd/', 1250 );
				return false;
			}
			
			if (empty ( $new_pwdconfirm )) {
				$this->flash ( '请再输入一次新密码!', '/Users/editpwd/', 1250 );
				return false;
			}
			
			if ($new_password != $new_pwdconfirm) {
				$this->flash ( '两次输入的密码不一致，请再次输入!', '/Users/editpwd/', 1250 );
				return false;
			}
			
			$data ['Admin'] ['password'] = md5 ( $new_pwdconfirm );
			$data ['Admin'] ['uid'] = $this->Session->read ( 'admin_uid' );
			
			$this->loadModel ( "Admin" );
			if ($this->Admin->edit ( $data )) {
				
				// 记录操作日志
				$this->AdminLog->addLog ( '修改密码,用户uid:' . $this->Session->read ( 'admin_uid' ) );
				
				$this->flash ( '密码修改成功!', '/Users/editpwd/', 1250 );
				return false;
			} else {
				$this->flash ( '密码修改失败!', '/Users/editpwd/', 1250 );
				return false;
			}
		} else {
			// 加载Admin model
			$this->loadModel ( 'Admin' );
			$AdminInfo = $this->Admin->GetAdminByID ( $this->Session->read ( 'admin_uid' ) );
			$AdminInfo ['Admin'] ['lastlogintime'] = date ( "Y-m-d H:i:s", $AdminInfo ['Admin'] ['lastlogintime'] );
			$this->set ( 'AdminInfo', $AdminInfo );
		}
	}
	
	/**
	 * 左侧导航菜单
	 */
	public function left() {
		// 不使用布局
		$this->autoLayout = false;
		
		$mid = $this->params ['mid'];
		if (empty ( $mid ) || ! isset ( $mid )) {
			$mid = 1;
		}
		
		// 加载Admin model
		$this->loadModel ( 'Menu' );
		
		// 顶部导航菜单
		$datas = $this->Menu->admin_menu ( $mid );
		$left_menu = "";
		
		foreach ( $datas as $_value ) {
			
			$left_menu .= '<h3 class="f14"><span class="switchs cu on" title="展开与收缩"></span>' . $_value ['Menu'] ['name'] . '</h3>';
			
			$left_menu .= '<ul>';
			$sub_array = $this->Menu->admin_menu ( $_value ['Menu'] ['id'] );
			foreach ( $sub_array as $_key => $_m ) {
				// 附加参数
				$data = $_m ['Menu'] ['data'] ? '&' . $_m ['Menu'] ['data'] : '';
				if ($mid == 5) { // 左侧菜单不显示选中状态
					$classname = 'class="sub_menu"';
				} else {
					$classname = 'class="sub_menu"';
				}
				$left_menu .= '<li id="_MP' . $_m ['Menu'] ['id'] . '" ' . $classname . '><a href="javascript:_MP(' . $_m ['Menu'] ['id'] . ',\'' . $_m ['Menu'] ['controller'] . '\');" hidefocus="true" style="outline:none;">' . $_m ['Menu'] ['name'] . '</a></li>';
			}
			$left_menu .= '</ul>';
		}
		$this->set ( 'left_menu', $left_menu );
	}
}
?>